package pub.gll.onepeas.libbase.track.aop

import android.app.Activity
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import org.aspectj.lang.JoinPoint
import org.aspectj.lang.ProceedingJoinPoint
import org.aspectj.lang.annotation.*
import pub.gll.onepeas.libbase.BaseApp
import pub.gll.onepeas.libbase.track.annotation.ViewExposure
import pub.gll.onepeas.libbase.track.manager.KKVEManager

/**
 * <pre>
 * @author : whg
 * @e-mail : guanhuawei@kkworld.com
 * @time   : 2021/7/8 20:35
 * @desc   : view全曝光时机hook
 * 解决问题：在实现view全曝光时，通过在decorview和contentview之前加入了一个kktrackerexposurelayout，通过这个view来获取页面子view的各个时机。
 * 现在hook的时机，就是其他地方直接加入到contentview之上的view，使它和kktrackerexposurelayout在一层。
 * 所以通过hook该时机，使其添加的view也称为kktrackerexposurelayout的子view。
 * @version: 1.0
</pre> *
 */
@Aspect
class ViewExposureAop {
    @Pointcut("execution( * pub.gll.onepeas.todo.MainTestAct.*(..))")
    fun pointcut1() {

    }

    @Before("pointcut1()")
    fun pointcut1hook(joinPoint: JoinPoint) {

    }


//    @Pointcut("execution(* android.app.Activity.*(..))")
//    fun pointcut() {
//
//    }


    @Pointcut("execution(* androidx.appcompat.app.AppCompatActivity.*(..))")
    fun pointcut() {

    }

    @Around("pointcut()")
    fun hookFindViewById(joinPoint: ProceedingJoinPoint): Any? {
        val signatureName = joinPoint.signature.name
        Log.e("LLLLLLL", "signatureName=${signatureName}")
        if (joinPoint.signature.name != "findViewById"){
            return joinPoint.proceed()
        }
        val id = joinPoint.args[0] as Int

        Log.e("LLLLLLL", "android.R.id.content=${android.R.id.content}")
        Log.e("LLLLLLL", "id=${id}")

        val activity = joinPoint.target as Activity

        if (id != android.R.id.content) {
            return joinPoint.proceed(arrayOf(id))
        }

        val annotation: ViewExposure? = activity.javaClass.getAnnotation(ViewExposure::class.java)
        if (annotation == null) {
            return joinPoint.proceed(arrayOf(id))
        }

        val container: ViewGroup = joinPoint.proceed(arrayOf(id)) as ViewGroup
        return KKVEManager.getInstance().getKkViewExposureLayout(container, activity)
    }

}